home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 April / SGI IRIX 6.5 Applications 2004 April.iso / dist / mozilla.idb / var / netscape / mozilla / chrome / messenger.jar.z / messenger.jar / content / messenger-smime / certFetchingStatus.js next >
Text File  |  2003-11-11  |  8KB  |  292 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2002
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. /* We expect the following arguments:
  38.    - pref name of LDAP directory to fetch from
  39.    - array with email addresses
  40.  
  41.   Display modal dialog with message and stop button.
  42.   In onload, kick off binding to LDAP.
  43.   When bound, kick off the searches.
  44.   On finding certificates, import into permanent cert database.
  45.   When all searches are finished, close the dialog.
  46. */ 
  47.  
  48. const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  49. const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
  50. const CertAttribute = "usercertificate;binary";
  51.  
  52. var gEmailAddresses;
  53. var gDirectoryPref;
  54. var gLdapServerURL;
  55. var gLdapConnection;
  56. var gCertDB;
  57. var gLdapOperation;
  58.  
  59. function onLoad()
  60. {
  61.   gDirectoryPref = window.arguments[0];
  62.   gEmailAddresses = window.arguments[1];
  63.  
  64.   if (!gEmailAddresses.length)
  65.   {
  66.     window.close();
  67.     return;
  68.   }
  69.  
  70.   setTimeout(search, 1);
  71. }
  72.  
  73. function search()
  74. {
  75.   var prefService =
  76.     Components.classes["@mozilla.org/preferences-service;1"]
  77.       .getService(Components.interfaces.nsIPrefService);
  78.   var prefs = prefService.getBranch(null);
  79.  
  80.   gLdapServerURL = 
  81.     Components.classes["@mozilla.org/network/ldap-url;1"]
  82.       .createInstance().QueryInterface(Components.interfaces.nsILDAPURL);
  83.  
  84.   try {
  85.     gLdapServerURL.spec = prefs.getCharPref(gDirectoryPref + ".uri");
  86.  
  87.     gLdapConnection = Components.classes["@mozilla.org/network/ldap-connection;1"]
  88.       .createInstance().QueryInterface(Components.interfaces.nsILDAPConnection);
  89.  
  90.     gLdapConnection.init(
  91.       gLdapServerURL.asciiHost,
  92.       gLdapServerURL.port,
  93.       gLdapServerURL.options,
  94.       null,
  95.       getProxyOnUIThread(new boundListener(),
  96.                             Components.interfaces.nsILDAPMessageListener),
  97.       null);
  98.  
  99.   } catch (ex) {
  100.     window.close();
  101.   }
  102. }
  103.  
  104. function stopFetching()
  105. {
  106.   if (gLdapOperation) {
  107.     try {
  108.       gLdapOperation.abandon();
  109.     }
  110.     catch (e) {
  111.     }
  112.   }
  113.   window.close();
  114. }
  115.  
  116. function importCert(ber_value)
  117. {
  118.   if (!gCertDB) {
  119.     gCertDB = Components.classes[nsX509CertDB].getService(nsIX509CertDB);
  120.   }
  121.  
  122.   var cert_length = new Object();
  123.   var cert_bytes = ber_value.get(cert_length);
  124.  
  125.   if (cert_bytes) {
  126.     gCertDB.importEmailCertificate(cert_bytes, cert_length.value, null);
  127.   }
  128. }
  129.  
  130. function kickOffBind()
  131. {
  132.   try {
  133.     gLdapOperation = Components.classes["@mozilla.org/network/ldap-operation;1"]
  134.       .createInstance().QueryInterface(Components.interfaces.nsILDAPOperation);
  135.  
  136.     gLdapOperation.init(gLdapConnection,
  137.                         getProxyOnUIThread(new ldapMessageListener(),
  138.                             Components.interfaces.nsILDAPMessageListener),
  139.                         null);
  140.  
  141.     gLdapOperation.simpleBind(null);
  142.   }
  143.   catch (e) {
  144.     window.close();
  145.   }
  146. }
  147.  
  148. function kickOffSearch()
  149. {
  150.   try {
  151.     var prefix1 = "";
  152.     var suffix1 = "";
  153.  
  154.     var urlFilter = gLdapServerURL.filter;
  155.  
  156.     if (urlFilter != null && urlFilter.length > 0 && urlFilter != "(objectclass=*)") {
  157.       if (urlFilter[0] == '(') {
  158.         prefix1 = "(&" + urlFilter;
  159.       }
  160.       else {
  161.         prefix1 = "(&(" + urlFilter + ")";
  162.       }
  163.       suffix1 = ")";
  164.     }
  165.  
  166.     var prefix2 = "";
  167.     var suffix2 = "";
  168.  
  169.     if (gEmailAddresses.length > 1) {
  170.       prefix2 = "(|";
  171.       suffix2 = ")";
  172.     }
  173.  
  174.     var mailFilter = "";
  175.  
  176.     for (var i = 0; i < gEmailAddresses.length; ++i) {
  177.       mailFilter += "(mail=" + gEmailAddresses[i] + ")";
  178.     }
  179.     
  180.     var filter = prefix1 + prefix2 + mailFilter + suffix2 + suffix1;
  181.  
  182.     var wanted_attributes = new Array();
  183.     wanted_attributes[0] = CertAttribute;
  184.  
  185.     // Max search results =>
  186.     // Double number of email addresses, because each person might have
  187.     // multiple certificates listed. We expect at most two certificates,
  188.     // one for signing, one for encrypting.
  189.     // Maybe that number should be larger, to allow for deployments,
  190.     // where even more certs can be stored per user???
  191.     
  192.     var maxEntriesWanted = gEmailAddresses.length * 2;
  193.  
  194.     gLdapOperation.searchExt(gLdapServerURL.dn, gLdapServerURL.scope,
  195.                              filter, 1, wanted_attributes, 0, maxEntriesWanted);
  196.   }
  197.   catch (e) {
  198.     window.close();
  199.   }
  200. }
  201.  
  202.  
  203. function boundListener() {
  204. }
  205.  
  206. boundListener.prototype.QueryInterface =
  207.   function(iid) {
  208.     if (!iid.equals(Components.interfaces.nsISupports) &&
  209.         !iid.equals(Components.interfaces.nsILDAPMessageListener))
  210.         throw Components.results.NS_ERROR_NO_INTERFACE;
  211.  
  212.     return this;
  213.   }
  214.  
  215. boundListener.prototype.onLDAPMessage = 
  216.   function(aMessage) {
  217.   }
  218.  
  219. boundListener.prototype.onLDAPInit = 
  220.   function(aConn, aStatus) {
  221.     kickOffBind();
  222.   }
  223.  
  224.  
  225. function ldapMessageListener() {
  226. }
  227.  
  228. ldapMessageListener.prototype.QueryInterface =
  229.   function(iid) {
  230.     if (!iid.equals(Components.interfaces.nsISupports) &&
  231.         !iid.equals(Components.interfaces.nsILDAPMessageListener))
  232.         throw Components.results.NS_ERROR_NO_INTERFACE;
  233.  
  234.     return this;
  235.   }
  236.  
  237. ldapMessageListener.prototype.onLDAPMessage = 
  238.   function(aMessage) {
  239.     if (Components.interfaces.nsILDAPMessage.RES_SEARCH_RESULT == aMessage.type) {
  240.       window.close();
  241.       return;
  242.     }
  243.  
  244.     if (Components.interfaces.nsILDAPMessage.RES_BIND == aMessage.type) {
  245.       if (Components.interfaces.nsILDAPErrors.SUCCESS != aMessage.errorCode) {
  246.         window.close();
  247.       }
  248.       else {
  249.         kickOffSearch();
  250.       }
  251.       return;
  252.     }
  253.  
  254.     if (Components.interfaces.nsILDAPMessage.RES_SEARCH_ENTRY == aMessage.type) {
  255.       var outSize = new Object();
  256.       try {
  257.         var outBinValues = aMessage.getBinaryValues(CertAttribute, outSize);
  258.  
  259.         var i;
  260.         for (i=0; i < outSize.value; ++i) {
  261.           importCert(outBinValues[i]);
  262.         }
  263.       }
  264.       catch (e) {
  265.       }
  266.       return;
  267.     }
  268.   }
  269.  
  270. ldapMessageListener.prototype.onLDAPInit = 
  271.   function(aConn, aStatus) {
  272.   }
  273.  
  274.  
  275. function getProxyOnUIThread(aObject, aInterface) {
  276.     var eventQSvc = Components.
  277.             classes["@mozilla.org/event-queue-service;1"].
  278.             getService(Components.interfaces.nsIEventQueueService);
  279.  
  280.     var uiQueue = eventQSvc.
  281.             getSpecialEventQueue(Components.interfaces.
  282.             nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
  283.  
  284.     var proxyMgr = Components.
  285.             classes["@mozilla.org/xpcomproxy;1"].
  286.             getService(Components.interfaces.nsIProxyObjectManager);
  287.  
  288.     return proxyMgr.getProxyForObject(uiQueue, 
  289.             aInterface, aObject, 5); 
  290.     // 5 == PROXY_ALWAYS | PROXY_SYNC
  291. }
  292.